import { notification } from 'antd';
import { 
  create, update, queryPage, 
  queryInfoById, remove, queryLocationList, 
  queryOptionList, queryNameList, queryTemple, importFile 
} from './service';
import { routerRedux } from 'dva/router';

import { queryDictList, queryDictName } from '@/services/dict';



export default {
  namespace: 'archives_point',

  state: {
    total: {},
    dict: {},
    typeData: {},
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put, select }) {
      const cond = yield select(state => state.archives_point.cond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setCond', payload: params });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.archives_point.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      const sub = yield call(queryDictName, {
        key: 'fire.device.type',
        prefix: '60001',
      })
      yield put({ type: 'setDict', payload: {...dict, sub} })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};

      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put(routerRedux.goBack());
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.goBack());
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload);
      yield put({ type: 'queryPage' });
    },
    *queryLocation(_, { call, put }) {
      const response = yield call(queryLocationList);
      yield put({ type: 'setLocationData', payload: response })
    },
    *optionLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'setOptionData', payload: response })
    },
    *areaLocation({ payload }, { call, put }) {
      const response = yield call(queryOptionList, payload);
      yield put({ type: 'setAreaOption', payload: response });
    },
    *queryNameList({ payload }, { call, put }) {
      const response = yield call(queryNameList, payload);
      yield put({ type: 'setNameList', payload: response });
    },
    *queryTypeOption({ payload }, { call, put }) {
      const response = yield call(queryNameList, payload);
      yield put({ type: 'setNameOption', payload: response });
    },
    *queryTemple({ payload }, { call, put }) {

      const { list } = yield call(queryTemple, { name: '重点部位' });
      const ans = {
        ...(Array.isArray(list) && list[0] ? list[0] : {}),
        name: '重点部位',
        ...payload
      }
      yield put({ 
        type: 'setTemple', 
        payload: ans
      });
      yield put({ type: 'handleShow', payload: true})
    },
    *importFile({ payload }, { call, put }) {
      yield put({ type: 'handleShow', payload: false})
      const msg = yield call(importFile, payload)
      yield put({ type: `queryPage` })
      msg && notification.success({
        message: '保存成功',
        description: msg
      });
    },
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setLocationData(state, action) {
      return {
        ...state,
        locationData: action.payload
      }
    },
    setOptionData(state, action) {
      return {
        ...state,
        optionData: action.payload
      }
    },
    setAreaOption(state, action) {
      return {
        ...state,
        areaData: action.payload
      }
    },
    setNameList(state, action) {
      return {
        ...state,
        typeData: action.payload
      }
    },
    setNameOption(state, action) {
      return {
        ...state,
        typeOption: action.payload
      }
    },
    setTemple(state, action) {
      return {
        ...state,
        temple: action.payload
      }
    },
    handleShow(state, action) {
      return {
        ...state,
        show: action.payload
      }
    }
  }
}
